package com.sphd.Restaurant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sphd.Restaurant.R;
import com.sphd.Restaurant.base.BaseActivity;
import com.sphd.Restaurant.infos.AddressInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.NewAddressP;
import com.sphd.Restaurant.utils.AppManager;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.utils.MyUtils;
import com.sphd.Restaurant.views.ClearEditText;
import com.sphd.Restaurant.views.NewAddressView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

public class NewAddressActivity extends BaseActivity implements NewAddressView {
    Intent intent;
    Bundle bundle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_address_name)
    ClearEditText etAddressName;
    @BindView(R.id.et_address_phone)
    ClearEditText etAddressPhone;
    @BindView(R.id.et_address_address)
    ClearEditText etAddressAddress;
    @BindView(R.id.tv_clean)
    Button tvClean;
    AddressInfo addressInfo;
    NewAddressP newAddressP;
    @BindView(R.id.btn_ok)
    TextView btnOk;
    int TYPE = 0; //当 0时 修改界面 1 时候为新建界面 当为2为 为从AffirmOrderActivity跳过来的新建
    String succedHint;
    String errorHint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_address);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        TYPE = bundle.getInt(Constants.TYPE);
        Log.e("TYPE===",TYPE+"");
        if (TYPE == 0) {
            setToolBar(toolbar, "修改收餐地址", true);
            addressInfo = (AddressInfo) bundle.getSerializable(Constants.ADDRESSES);
            etAddressName.setText(addressInfo.getUser_name());
            etAddressPhone.setText(addressInfo.getUser_phone());
            etAddressAddress.setText(addressInfo.getAddr());
        } else {
            setToolBar(toolbar, "新建收餐地址", true);
            tvClean.setVisibility(View.GONE);
        }
        newAddressP = new NewAddressP(this);
        toolbar.setNavigationOnClickListener(view -> AppManager.getAppManager().finishActivity(this));
    }

    @OnClick(R.id.tv_clean)
    public void onClick() {
        tvClean.setEnabled(false);
        new SweetAlertDialog(NewAddressActivity.this, SweetAlertDialog.WARNING_TYPE)
                .setTitleText("是否删除此地址")
                .setConfirmText("确定")
                .setCancelText("取消")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        deleteAddr();
                    }
                }).setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
                tvClean.setEnabled(true);
            }
        })
                .show();


    }

    private void deleteAddr() {
        succedHint = "删除成功";
        errorHint = "删除失败";
        newAddressP.addData(Model.HTTPURL + Model.DELETADDRESS + "&addrid=" + addressInfo.getId());
    }

    private void finishAddr() {
        if (TYPE == 0) {
            succedHint = "修改成功";
            errorHint = "修改失败";
            newAddressP.addData(Model.HTTPURL + Model.UPDATEADDRESS + "userphone=" + turnUTF8(etAddressPhone.getText().toString())
                    + "&userid=" + getInt(Constants.USER_ID) + "&addr=" + turnUTF8(etAddressAddress.getText().toString())
                    + "&addrid=" + addressInfo.getId() + "&name=" + turnUTF8(etAddressName.getText().toString()));
        } else {
            succedHint = "添加成功";
            errorHint = "添加失败";
            newAddressP.addData(Model.HTTPURL + Model.ADDADDRESS + "userphone=" + turnUTF8(etAddressPhone.getText().toString())
                    + "&userid=" + getInt(Constants.USER_ID) + "&addr=" + turnUTF8(etAddressAddress.getText().toString())
                    + "&name=" + turnUTF8(etAddressName.getText().toString()));
        }
    }

    @Override
    public void succed() {
        new SweetAlertDialog(NewAddressActivity.this,
                SweetAlertDialog.SUCCESS_TYPE).setTitleText(succedHint).
                setConfirmClickListener(sweetAlertDialog -> {
                    sweetAlertDialog.dismiss();
                    if (TYPE == 2) {
                        setResult(0);
                    } else {
                        setResult(1);
                    }
                    tvClean.setEnabled(true);
                    AppManager.getAppManager().finishActivity(this);
                }).show();

    }

    @Override
    public void errored() {
        showAlerDialog(1, errorHint);
        tvClean.setEnabled(true);
    }

    @Override
    public void showdialog() {

    }

    @Override
    public void dissmissdialog() {

    }

    @OnClick(R.id.btn_ok)
    public void btnOk() {
        if (!TextUtils.isEmpty(etAddressName.getText().toString().trim())) {
            if (!TextUtils.isEmpty(etAddressPhone.getText().toString().trim())) {
                if (MyUtils.isMobileNO(etAddressPhone.getText().toString().trim())) {
                    if (!TextUtils.isEmpty(etAddressAddress.getText().toString().trim())) {
                        finishAddr();
                    } else {
                        etAddressAddress.setError("收餐地址不能为空");
                    }
                } else {
                    etAddressPhone.setError("请输入正确的手机号码");
                }
            } else {
                etAddressPhone.setError("手机号码不能为空");

            }
        } else {
            etAddressName.setError("名称不能为空");
        }

    }


}
